/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FormPembelian.java
 *
 * Created on 11 Nov 11, 21:24:02
 */
package Pembelian;

import MenuUtamaSistem.Login;
import MenuUtamaSistem.MenuUtamaController;
import java.awt.event.KeyEvent;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JOptionPane;

/**
 *
 * @author asus
 */
public class FormPembelian extends javax.swing.JDialog {

    /** Creates new form FormPembelian */
    public FormPembelian() throws Exception {

        initComponents();
        idUser.setText(Login.getLog().getUser());
        PembelianController.showDateAndClock(tanggalbeli, jamLabel1);
        noBeliText.setText(pembelian.getKodeAwal());

        try {
            List<pembelian> listBeli;
            listBeli = PembelianController.getBarangKoneksi().detailNOmor();
            tabelPembelianModel model = new tabelPembelianModel(listBeli);
            tabelbeli.setModel(model);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(FormPembelian.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(FormPembelian.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(FormPembelian.class.getName()).log(Level.SEVERE, null, ex);
        }


    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        editDialog = new javax.swing.JDialog();
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        nomorTextcari = new javax.swing.JTextField();
        Cari = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tabelbeli = new javax.swing.JTable();
        jPanel1 = new javax.swing.JPanel();
        formBeli = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        labelJam = new javax.swing.JLabel();
        logoLabel = new javax.swing.JLabel();
        backButton = new javax.swing.JButton();
        saveButton = new javax.swing.JButton();
        editButton = new javax.swing.JButton();
        logo = new javax.swing.JLabel();
        dataBeliPanel = new javax.swing.JPanel();
        noBeliText = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jumlahBeliText = new javax.swing.JTextField();
        hargaBeliText = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jamLabel1 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        idUser = new javax.swing.JLabel();
        tanggalbeli = new javax.swing.JLabel();
        batalbutton = new javax.swing.JButton();
        BG = new javax.swing.JLabel();

        editDialog.setBackground(new java.awt.Color(204, 204, 255));
        editDialog.setResizable(false);
        editDialog.getContentPane().setLayout(null);

        jPanel2.setBackground(new java.awt.Color(255, 255, 255));
        jPanel2.setLayout(null);

        jLabel1.setText("Masukkan nomor pembelian ");
        jPanel2.add(jLabel1);
        jLabel1.setBounds(20, 20, 150, 30);

        nomorTextcari.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nomorTextcariActionPerformed(evt);
            }
        });
        jPanel2.add(nomorTextcari);
        nomorTextcari.setBounds(170, 20, 112, 30);

        Cari.setText("CARI");
        Cari.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CariActionPerformed(evt);
            }
        });
        jPanel2.add(Cari);
        Cari.setBounds(320, 30, 80, 23);

        tabelbeli.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null},
                {null, null},
                {null, null},
                {null, null}
            },
            new String [] {
                "Tanggal Pembelian", "Nomor Pembelian"
            }
        ));
        tabelbeli.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tabelbeliMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tabelbeli);

        jPanel2.add(jScrollPane1);
        jScrollPane1.setBounds(30, 80, 400, 170);

        editDialog.getContentPane().add(jPanel2);
        jPanel2.setBounds(0, 0, 460, 270);

        setTitle("Form Pembelian - SP3EF");

        jPanel1.setBackground(new java.awt.Color(153, 204, 255));
        jPanel1.setLayout(null);

        formBeli.setFont(new java.awt.Font("Tempus Sans ITC", 1, 24));
        formBeli.setText("FORM PEMBELIAN");
        jPanel1.add(formBeli);
        formBeli.setBounds(10, 30, 280, 33);
        jPanel1.add(jSeparator1);
        jSeparator1.setBounds(0, 88, 560, 2);
        jPanel1.add(labelJam);
        labelJam.setBounds(500, 90, 0, 0);

        logoLabel.setText("@MAFY com 2011");
        jPanel1.add(logoLabel);
        logoLabel.setBounds(230, 420, 140, 20);

        backButton.setText("Kembali Ke Menu Utama");
        backButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                backButtonActionPerformed(evt);
            }
        });
        jPanel1.add(backButton);
        backButton.setBounds(20, 380, 150, 30);

        saveButton.setText("Simpan");
        saveButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveButtonActionPerformed(evt);
            }
        });
        jPanel1.add(saveButton);
        saveButton.setBounds(470, 380, 80, 30);

        editButton.setText("EDIT");
        editButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                editButtonActionPerformed(evt);
            }
        });
        jPanel1.add(editButton);
        editButton.setBounds(370, 380, 90, 30);

        logo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Pembelian/HP.png"))); // NOI18N
        jPanel1.add(logo);
        logo.setBounds(450, 0, 110, 90);

        dataBeliPanel.setBackground(new java.awt.Color(255, 255, 255));
        dataBeliPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Data Pembelian"));
        dataBeliPanel.setLayout(null);

        noBeliText.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                noBeliTextKeyReleased(evt);
            }
        });
        dataBeliPanel.add(noBeliText);
        noBeliText.setBounds(180, 60, 150, 20);

        jLabel4.setText("Nomor Pembelian");
        dataBeliPanel.add(jLabel4);
        jLabel4.setBounds(30, 60, 120, 14);

        jLabel5.setText("Jumlah Pembelian");
        dataBeliPanel.add(jLabel5);
        jLabel5.setBounds(30, 90, 130, 14);

        jumlahBeliText.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                jumlahBeliTextKeyTyped(evt);
            }
        });
        dataBeliPanel.add(jumlahBeliText);
        jumlahBeliText.setBounds(180, 90, 150, 20);

        hargaBeliText.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                hargaBeliTextKeyTyped(evt);
            }
        });
        dataBeliPanel.add(hargaBeliText);
        hargaBeliText.setBounds(180, 120, 150, 20);

        jLabel6.setText("Harga Pembelian");
        dataBeliPanel.add(jLabel6);
        jLabel6.setBounds(30, 120, 130, 14);

        jPanel1.add(dataBeliPanel);
        dataBeliPanel.setBounds(20, 100, 350, 240);

        jLabel7.setText("Jam");
        jPanel1.add(jLabel7);
        jLabel7.setBounds(400, 140, 50, 20);

        jLabel2.setText("Tanggal");
        jPanel1.add(jLabel2);
        jLabel2.setBounds(400, 110, 60, 20);
        jPanel1.add(jamLabel1);
        jamLabel1.setBounds(460, 140, 90, 20);

        jLabel9.setText("ID User");
        jPanel1.add(jLabel9);
        jLabel9.setBounds(400, 170, 50, 20);
        jPanel1.add(idUser);
        idUser.setBounds(460, 170, 90, 20);
        jPanel1.add(tanggalbeli);
        tanggalbeli.setBounds(460, 110, 90, 20);

        batalbutton.setText("Batal");
        batalbutton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                batalbuttonActionPerformed(evt);
            }
        });
        jPanel1.add(batalbutton);
        batalbutton.setBounds(180, 380, 90, 30);

        BG.setIcon(new javax.swing.ImageIcon(getClass().getResource("/MenuUtamaSistem/BG.jpg"))); // NOI18N
        jPanel1.add(BG);
        BG.setBounds(0, 0, 564, 454);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 564, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 454, Short.MAX_VALUE)
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-580)/2, (screenSize.height-492)/2, 580, 492);
    }// </editor-fold>//GEN-END:initComponents

    private void backButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_backButtonActionPerformed
        // TODO add your handling code here:
        this.setVisible(false);
        Login.getMain().setVisible(true);
    }//GEN-LAST:event_backButtonActionPerformed

    private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveButtonActionPerformed
        SimpleDateFormat form = new SimpleDateFormat("dd-MMM-yy");
        if (saveButton.getText().equalsIgnoreCase("Simpan")) {
//            if (noBeliText.getText().equalsIgnoreCase("MAFYB-") || jumlahBeliText.getText() == null || hargaBeliText.getText() == null) {
//                System.out.println("Benar");
//                JOptionPane.showMessageDialog(null, "Field harus diisi semua");
//            }
//        else {

//            beli.setTanggal(tglbeli);

//           beli.setWaktu(kode)

            try {
                System.out.println("salah");
                String kode = noBeliText.getText();
                String waktu = form.format(new GregorianCalendar().getTime());
                pembelian beli = new pembelian();
                beli.setNo_transaksibeli(kode);
                beli.setJumlah_beli(jumlahBeliText.getText());
                beli.setHarga_pembelian(hargaBeliText.getText());
                beli.setWaktu(waktu);
                beli.setIdUser(idUser.getText());
                PembelianController kontrolbeli;
                kontrolbeli = PembelianController.getBarangKoneksi();
                int status = kontrolbeli.tambahPembelian(beli);
                if (status == 0) {
                    JOptionPane.showMessageDialog(null, "Data pembelian berhasil ditambah");
                    noBeliText.setText(pembelian.getKodeAwal());
                    jumlahBeliText.setText("");
                    hargaBeliText.setText("");
                } else {
                    JOptionPane.showMessageDialog(null, "Transaksi sudah ada, data gagal disimpan");
                }

            } catch (SQLException ex) {
                Logger.getLogger(FormPembelian.class.getName()).log(Level.SEVERE, null, ex);
                JOptionPane.showMessageDialog(null, "Data Gagal disimpan");
            } catch (FileNotFoundException ex) {
                Logger.getLogger(FormPembelian.class.getName()).log(Level.SEVERE, null, ex);
                JOptionPane.showMessageDialog(null, "Data Gagal disimpan");
            } catch (IOException ex) {
                Logger.getLogger(FormPembelian.class.getName()).log(Level.SEVERE, null, ex);
                JOptionPane.showMessageDialog(null, "Data Gagal disimpan");
            } catch (Exception ex) {
                JOptionPane.showMessageDialog(null,"ERROR :"+ ex.getMessage());
            }

        } //        }
        else if (saveButton.getText().equalsIgnoreCase("Update")) {
//            int jum = Integer.parseInt(jumlahBeliText.getText());
//            int harga = Integer.parseInt(hargaBeliText.getText());
            pembelian beli = new pembelian();

            try {
                beli.setNo_transaksibeli(noBeliText.getText());
                beli.setJumlah_beli(jumlahBeliText.getText());
                beli.setHarga_pembelian(hargaBeliText.getText());
                PembelianController kontrol2 = PembelianController.getBarangKoneksi();
                kontrol2.updatePembelian(beli);
                JOptionPane.showMessageDialog(null, "Data berhasil di update");
                saveButton.setText("Simpan");
                noBeliText.setEditable(true);
                noBeliText.setText(pembelian.getKodeAwal());
                jumlahBeliText.setText("");
                hargaBeliText.setText("");
            } catch (SQLException ex) {
                Logger.getLogger(FormPembelian.class.getName()).log(Level.SEVERE, null, ex);
                JOptionPane.showMessageDialog(null, "Data tidak berhasil di update");
            } catch (FileNotFoundException ex) {
                Logger.getLogger(FormPembelian.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(FormPembelian.class.getName()).log(Level.SEVERE, null, ex);
            } catch (Exception ex) {
                JOptionPane.showMessageDialog(null,"ERROR :"+ ex.getMessage());
            }
        }
    }//GEN-LAST:event_saveButtonActionPerformed

    private void editButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_editButtonActionPerformed
        // TODO add your handling code here:
        try {
            List<pembelian> listBeli;
            listBeli = PembelianController.getBarangKoneksi().detailNOmor();
            tabelPembelianModel model = new tabelPembelianModel(listBeli);
            tabelbeli.setModel(model);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(FormPembelian.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(FormPembelian.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(FormPembelian.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage() + "");
        }

        editDialog.setVisible(true);
        editDialog.setSize(450, 300);
        saveButton.setText("Update");


    }//GEN-LAST:event_editButtonActionPerformed

    private void CariActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CariActionPerformed
        // TODO add your handling code here:
        pembelian beli = new pembelian();
        noBeliText.setEditable(false);
        try {
            String kode = nomorTextcari.getText().toUpperCase();
            beli.setNo_transaksibeli(kode);
            try {
                beli = PembelianController.getBarangKoneksi().cariNomor(beli);
                String notrans = beli.getNo_transaksibeli();
                int jumlah = beli.getJumlah_beli();
                int harga = beli.getHarga_pembelian();
                noBeliText.setText(notrans);
                jumlahBeliText.setText(String.valueOf(jumlah));
                hargaBeliText.setText(String.valueOf(harga));
                JOptionPane.showMessageDialog(null, "Kode " + kode + " berhasi ditemukan!");
            } catch (FileNotFoundException ex) {
                Logger.getLogger(FormPembelian.class.getName()).log(Level.SEVERE, null, ex);
                JOptionPane.showMessageDialog(null, "Kode " + kode + " tidak berhasi ditemukan!");
            } catch (IOException ex) {
                Logger.getLogger(FormPembelian.class.getName()).log(Level.SEVERE, null, ex);
                JOptionPane.showMessageDialog(null, "Kode " + kode + " tidak berhasi ditemukan!");
            } catch (SQLException ex) {
                Logger.getLogger(FormPembelian.class.getName()).log(Level.SEVERE, null, ex);
                JOptionPane.showMessageDialog(null, "Kode " + kode + " tidak berhasi ditemukan!");
            }
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage() + "");
        }
        editDialog.setVisible(false);
    }//GEN-LAST:event_CariActionPerformed

    private void nomorTextcariActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nomorTextcariActionPerformed
        // TODO add your handling code here:
        Cari.doClick();
    }//GEN-LAST:event_nomorTextcariActionPerformed

    private void batalbuttonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_batalbuttonActionPerformed
        // TODO add your handling code here:
//        batalbutt.doClick();
        saveButton.setText("Simpan");
        noBeliText.setText(pembelian.getKodeAwal());
        jumlahBeliText.setText("");
        hargaBeliText.setText("");
        noBeliText.setEditable(true);
    }//GEN-LAST:event_batalbuttonActionPerformed

    private void jumlahBeliTextKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jumlahBeliTextKeyTyped
        // TODO add your handling code here:
//        if (Character.isLetter((evt.getKeyChar()))) {
//            evt.consume();
//            JOptionPane.showMessageDialog(null, "Inputkan angka");
//
//        }
    }//GEN-LAST:event_jumlahBeliTextKeyTyped

    private void hargaBeliTextKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_hargaBeliTextKeyTyped
        // TODO add your handling code here:
//        if (Character.isLetter((evt.getKeyChar()))) {
//            evt.consume();
//            JOptionPane.showMessageDialog(null, "Inputkan angka");
//
//        }
    }//GEN-LAST:event_hargaBeliTextKeyTyped

    private void noBeliTextKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_noBeliTextKeyReleased
        // TODO add your handling code here:
        saveButton.setEnabled(true);
        editButton.setEnabled(true);
//        if (noBeliText.getText().length() < 6) {
//            noBeliText.setText(pembelian.getKodeAwal());
//            JOptionPane.showMessageDialog(null, "Dilarang Menghapus kode awal");
//        }
    }//GEN-LAST:event_noBeliTextKeyReleased

    private void tabelbeliMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tabelbeliMouseClicked
        // TODO add your handling code here:
        int row = tabelbeli.getSelectedRow();
        nomorTextcari.setText(tabelbeli.getValueAt(row, 0).toString());

    }//GEN-LAST:event_tabelbeliMouseClicked

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                try {
                    new FormPembelian().setVisible(true);
                } catch (Exception ex) {
                    Logger.getLogger(FormPembelian.class.getName()).log(Level.SEVERE, null, ex);
                }

            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel BG;
    private javax.swing.JButton Cari;
    private javax.swing.JButton backButton;
    private javax.swing.JButton batalbutton;
    private javax.swing.JPanel dataBeliPanel;
    private javax.swing.JButton editButton;
    private javax.swing.JDialog editDialog;
    private javax.swing.JLabel formBeli;
    private javax.swing.JTextField hargaBeliText;
    private javax.swing.JLabel idUser;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JLabel jamLabel1;
    private javax.swing.JTextField jumlahBeliText;
    private javax.swing.JLabel labelJam;
    private javax.swing.JLabel logo;
    private javax.swing.JLabel logoLabel;
    private javax.swing.JTextField noBeliText;
    private javax.swing.JTextField nomorTextcari;
    private javax.swing.JButton saveButton;
    private javax.swing.JTable tabelbeli;
    private javax.swing.JLabel tanggalbeli;
    // End of variables declaration//GEN-END:variables
}
